package com.nyist.utils;

import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

/**
 * 1.
 *
 * @ClassName HtmlToJson
 * @Description
 * @Author xiaoyang
 * @Date 2023/04/25/10:54
 */
@Slf4j
public class HtmlToJson {

    @Autowired
    private RestTemplate restTemplate;

    public String sendGetHtml(String baseUrl, String params) {
        String targetUrl = baseUrl + "?" + params;
        try {
            Document doc = Jsoup.connect(targetUrl).get();
            log.info("====>>>title: {}",doc.title());

            // Elements elementsByTag = doc.getElementsByTag("input");
            // // 打印
            // log.info("====>>>elementsByTag: {}",elementsByTag);
            //
            // for (Element element : elementsByTag) {
            //     // 打印
            //     log.info("====>>>element: {}",element);
            //     String name = element.attr("name");
            //     // 打印
            //     log.info("====>>>element===name: {}",name);
            // }

            Elements elementsByClass = doc.getElementsByClass("form-group col-auto");

            // 打印
            log.info("====>>>byClass: {}",elementsByClass);
            Element element1 = elementsByClass.get(0);
            Elements elementsByTag = element1.getElementsByTag("input");
            // 打印
            log.info("====>>>elementsByTag: {}",elementsByTag);

            for (Element element : elementsByTag) {
                // 打印
                log.info("====>>>element: {}",element);
                String name = element.attr("name");
                // 打印
                log.info("====>>>element===name: {}",name);
            }

            // 使用选择器语法查找元素
            // Elements newsHeadlines = doc.select("a.badge");
            // for (Element headline : newsHeadlines) {
            //     // 获取元素的链接地址
            //     String href = headline.absUrl("href");
            //     // 打印
            //     log.info("====>>>href: {}",href);
            // }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return "";
    }

    public static void main(String[] args) {
        String baseUrl = "https://cloud.tradelinksig.com/cas/login";
        String params = "service=https://cloud.tradelinksig.com/rocars&locale=zh_CN";

        HtmlToJson htmlToJson = new HtmlToJson();
        String s = htmlToJson.sendGetHtml(baseUrl, params);

    }

}
